using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class uicontrol_CT_KinhTe : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtNam.Text = DateTime.Now.Year.ToString();
            ResetForm();
            Bind_Data();
        }
    }

    public void Bind_Data()
    {
        dsExist.SelectParameters.Clear();
        dsExist.SelectParameters.Add("MaSanPham", ddlSP.SelectedValue);
        dsExist.SelectParameters.Add("MaHoiDong", ddlHD.SelectedValue);
        DataTable dt = ((DataView)dsExist.Select(DataSourceSelectArguments.Empty)).Table;
        if (dt.Rows.Count > 0)
        {
            ddlSP.SelectedValue = dt.Rows[0]["MaSanPham"].ToString();
            ddlHD.SelectedValue = dt.Rows[0]["MaHoiDong"].ToString();

            txtTDT.Text = dt.Rows[0]["Q"].ToString();
            txtbH.Text = dt.Rows[0]["BetaH"].ToString();
            txtbI.Text = dt.Rows[0]["BetaI"].ToString();
            txtbO.Text = dt.Rows[0]["BetaO"].ToString();
            txtbT.Text = dt.Rows[0]["BetaT"].ToString();
            txtGhiChu.Text = dt.Rows[0]["GhiChu"].ToString();
            txtNam.Text = dt.Rows[0]["Nam"].ToString();
        }
        else
        {
            txtbH.Text = "0";
            txtbI.Text = "0";
            txtbO.Text = "0";
            txtbT.Text = "0";
            txtGhiChu.Text = "";
            txtNam.Text = DateTime.Now.Year.ToString();
            txtTDT.Text = "0";            
        }

        dt.Dispose();
    }
    protected void btnDongY_Click(object sender, EventArgs e)
    {
        dsExist.SelectParameters.Clear();
        dsExist.SelectParameters.Add("MaSanPham", ddlSP.SelectedValue);
        dsExist.SelectParameters.Add("MaHoiDong", ddlHD.SelectedValue);
        DataTable dt = ((DataView)dsExist.Select(DataSourceSelectArguments.Empty)).Table;
        if (dt.Rows.Count > 0)
        {
            dsExist.Update();
        }
        else
        {

            dsExist.Insert();
            btnDongY.Enabled = false;
        }
        dt.Dispose();
        ResetForm();
    }

    #region Reset
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ResetForm();
    }

    public void ResetForm()
    {
        btnDongY.Enabled = true;
        ddlHD.DataBind();
        ddlSP.DataBind();

        txtbH.Text = "0";
        txtbI.Text = "0";
        txtbO.Text = "0";
        txtbT.Text = "0";
        txtGhiChu.Text = "";
        txtNam.Text = DateTime.Now.Year.ToString();
        txtTDT.Text = "0";
        ddlHD.SelectedIndex = 0;
        ddlSP.SelectedIndex = 0;
    } 
    #endregion
    protected void ddlHD_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind_Data();
    }
    protected void ddlSP_SelectedIndexChanged(object sender, EventArgs e)
    {   
        Bind_Data();
    }   
   
}
